//
//  ChatBubble.swift
//  Zapsh
//
//  Created by admin on 28.04.2024.
//

import SwiftUI

struct ChatBubble: View {
    var body: some View {
        ZStack {
            VStack(spacing: 20) {
                BubbleText(title: "Hello^ my dear friend", color: Color.gray, dateOffset: 60)
                BubbleText(title: "Hi^ bro", color: .purple, dateOffset: 120)
                BubbleText(title: "Good luck bro", color: .gray, dateOffset: 240)
                BubbleText(title: "Ty^ u too!", color: .purple, dateOffset: 360)
            }
        }
    }
}

#Preview {
    ChatBubble()
}

struct BubbleText: View {
    
    let title: String?
    let color: Color?
    let date: Date
    let dateFormatter: DateFormatter
    
    var dateValue: String {
        dateFormatter.string(from: date)
    }
    
    init(title: String?, color: Color?, dateOffset: TimeInterval = 0) {
        self.title = title
        self.color = color
        date = Date().addingTimeInterval(dateOffset)
        dateFormatter = DateFormatter()
        dateFormatter.timeStyle = .short
    }
    
    var body: some View {
        VStack {
            Text(title ?? "")
                .fontWeight(.medium)
                .padding()
            Text(dateValue)
                .fontWeight(.light)
                .font(Font.system(size: 14))
                .foregroundStyle(.secondary)
        }
        .padding()
        .background(
            RoundedRectangle(cornerRadius: 25)
                .foregroundColor(color)
                .opacity(0.4)
                .shadow(radius: 20)
        )
    }
}
